package com.yingsheng.bi.hks.service.mine;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.enums.EnumConst;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.bo.InvoicesBo;
import com.yingsheng.bi.hks.bo.ResumeBo;
import com.yingsheng.bi.hks.bo.WorkExperience;
import com.yingsheng.bi.hks.dto.mine.*;
import com.yingsheng.bi.hks.mapping.InvoicesMgmtMappings;
import com.yingsheng.bi.hks.repository.dao.po.*;
import com.yingsheng.bi.hks.repository.dao.service.*;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class MineMgmtService {

    @Autowired
    private InvoicesDaoService invoicesDaoService;

    @Autowired
    private PartnerDaoService partnerDaoService;

    @Autowired
    private ResumeDetailsDaoService resumeDetailsDaoService;

    @Autowired
    private ResumeExceptsDaoService resumeExceptsDaoService;

    @Autowired
    private ResumeInfoDaoService resumeInfoDaoService;

    @Autowired
    private ResumeWorksDaoService resumeWorksDaoService;

    /**
     * 发票新增
     *
     * @param req
     * @return
     */
    public InvoicesAddRes invoicesAdd(InvoicesAddReq req) {
        Invoices invoices = InvoicesMgmtMappings.INSTANCE.addReq2po(req);
        invoicesDaoService.insert(invoices);
        return new InvoicesAddRes();
    }

    /**
     * 发票查询
     *
     * @param req
     * @return
     */
    public InvoicesPersonQryRes invoicesPersonQry(InvoicesPersonQryReq req) {
        PUser user = Subject.get();
        InvoicesPersonQryRes res = new InvoicesPersonQryRes();
        Invoices invoices = InvoicesMgmtMappings.INSTANCE.addReq2po(req);
        invoices.setUserId(user.getId());
        List<Invoices> invoicesList = invoicesDaoService.invoicesPersonQry(invoices);
        if (invoicesList.isEmpty()) {
            return res;
        }
        List<InvoicesBo> invoicesBoList = InvoicesMgmtMappings.INSTANCE.invoicesList2boList(invoicesList);
        res.setInvoicesBoList(invoicesBoList);
        return res;
    }

    public InterviewAuntQryRes interviewAuntQry(InterviewAuntQryReq req) {

        PUser user = Subject.get();

        List<String> ids = resumeInfoDaoService.selectResumeIdsByUserIdAndState(user.getId(), req.getState());

        InterviewAuntQryRes res = new InterviewAuntQryRes();
        List<ResumeBo> resumeBoList = new ArrayList<>();
        List<ResumeInfo> resumeInfoList = resumeInfoDaoService.qryByIds(ids);
        if (resumeInfoList.isEmpty()) {
            return res;
        }
        for (int i = 0; i < resumeInfoList.size(); i++) {
            ResumeBo resumeBo = new ResumeBo();
            ResumeInfo resumeInfo = resumeInfoList.get(i);
            resumeBo.setId(resumeInfo.getId());
            resumeBo.setWorkStatus(resumeInfo.getWorkStatus());
            resumeBo.setName(resumeInfo.getName());
            resumeBo.setMobile(resumeInfo.getMobile());
            resumeBo.setAvatar(resumeInfo.getAvatar());
            resumeBo.setDescription(resumeInfo.getDescription());
            resumeBo.setEducation(resumeInfo.getEducation());

            List<ResumeDetails> resumeDetailsList = resumeDetailsDaoService.query(resumeInfo.getId());
            for (int j = 0; j < resumeDetailsList.size(); j++) {

                ResumeDetails resumeDetails = resumeDetailsList.get(j);
                try {
                    Field nameField = ResumeBo.class.getDeclaredField(resumeDetails.getFieldName());
                    nameField.setAccessible(true); // 取消Java的访问控制检查
                    nameField.set(resumeBo, resumeDetails.getFieldValue());
                } catch (Exception e) {
                    res.setErrorCode("999999");
                    res.setErrorCode(e.getMessage());
                    return res;
                }
            }

            ResumeExpects resumeExpects = resumeExceptsDaoService.qryByResumeInfoId(resumeInfo.getId());
            if (!StringUtil.isNullOrEmpty(resumeExpects.getPostsion())) {
                resumeBo.setPosition(resumeExpects.getPostsion());
            }

            List<WorkExperience> workExperienceList = new ArrayList<>();
            List<ResumeWorksWithBLOBs> resumeWorksList = resumeWorksDaoService.qryByResumeInfoId(resumeInfo.getId());
            if (!resumeWorksList.isEmpty()) {
                for (int j = 0; j < resumeWorksList.size(); j++) {
                    WorkExperience workExperience = new WorkExperience();
                    ResumeWorksWithBLOBs resumeWorksWithBLOBs = resumeWorksList.get(j);
                    workExperience.setPosition(resumeWorksWithBLOBs.getPosition());
                    workExperience.setContent(resumeWorksWithBLOBs.getContent());
                    workExperience.setBegDate(resumeWorksWithBLOBs.getBegDate());
                    workExperience.setEndDate(resumeWorksWithBLOBs.getEndDate());
                    workExperienceList.add(workExperience);
                }
            }
            resumeBo.setWorkExperiences(workExperienceList);
            resumeBoList.add(resumeBo);
        }
        res.setResumeBoList(resumeBoList);
        return res;
    }

    /**
     * 合伙人登记
     *
     * @param req
     * @return
     */
    public PartnerAddRes partnerAdd(PartnerAddReq req) {
        Partner partner = new Partner();
        partner.setId(Utils.genUUid());
        partner.setName(req.getName());
        partner.setMobile(req.getMobile());
        partner.setRemark(req.getRemark());
        partner.setAddress(req.getAddress());
        partner.setState("0");
        partnerDaoService.insert(partner);
        return new PartnerAddRes();
    }

    public MyResumeQryRes myResumeQry(MyResumeQryReq req) {
        PUser user = Subject.get();
        MyResumeQryRes res = new MyResumeQryRes();

        if (!EnumConst.Role.P03.equals(user.getRoleId())){
            res.setReturnCode("999999");
            res.setReturnMsg("用户角色错误！");
        }

        ResumeBo resumeBo = new ResumeBo();
        ResumeInfo resumeInfo = resumeInfoDaoService.qryByUserId(user.getId());
        if (resumeInfo == null) {
            return res;
        }

        resumeBo.setId(resumeInfo.getId());
        resumeBo.setWorkStatus(resumeInfo.getWorkStatus());
        resumeBo.setName(resumeInfo.getName());
        resumeBo.setMobile(resumeInfo.getMobile());
        resumeBo.setAvatar(resumeInfo.getAvatar());
        resumeBo.setDescription(resumeInfo.getDescription());
        resumeBo.setEducation(resumeInfo.getEducation());
        resumeBo.setDescription(resumeInfo.getDescription());

        List<ResumeDetails> resumeDetailsList = resumeDetailsDaoService.query(resumeInfo.getId());
        for (int i = 0; i < resumeDetailsList.size(); i++) {

            ResumeDetails resumeDetails = resumeDetailsList.get(i);
            try {
                Field nameField = ResumeBo.class.getDeclaredField(resumeDetails.getFieldName());
                nameField.setAccessible(true); // 取消Java的访问控制检查
                nameField.set(resumeBo, resumeDetails.getFieldValue());
            } catch (Exception e) {
                res.setErrorCode("999999");
                res.setErrorCode(e.getMessage());
                return res;
            }
        }

        ResumeExpects resumeExpects = resumeExceptsDaoService.qryByResumeInfoId(resumeInfo.getId());
        if (!StringUtil.isNullOrEmpty(resumeExpects.getPostsion())) {
            resumeBo.setPosition(resumeExpects.getPostsion());
        }

        List<WorkExperience> workExperienceList = new ArrayList<>();
        List<ResumeWorksWithBLOBs> resumeWorksList = resumeWorksDaoService.qryByResumeInfoId(resumeInfo.getId());
        if (!resumeWorksList.isEmpty()) {
            for (int j = 0; j < resumeWorksList.size(); j++) {
                WorkExperience workExperience = new WorkExperience();
                ResumeWorksWithBLOBs resumeWorksWithBLOBs = resumeWorksList.get(j);
                workExperience.setPosition(resumeWorksWithBLOBs.getPosition());
                workExperience.setContent(resumeWorksWithBLOBs.getContent());
                workExperience.setBegDate(resumeWorksWithBLOBs.getBegDate());
                workExperience.setEndDate(resumeWorksWithBLOBs.getEndDate());
                workExperienceList.add(workExperience);
            }
        }
        resumeBo.setWorkExperiences(workExperienceList);

        res.setResumeBo(resumeBo);
        return res;
    }
}
